package com.google.client;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.DoubleBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;

public class AddProduct extends Composite {
	private DoubleBox txtPrice;
	private MarketingServiceAsync marketingSvc = GWT.create(MarketingService.class);

	public AddProduct() {
		
		FlexTable flexTable = new FlexTable();
		initWidget(flexTable);
		
		Label lblName = new Label("name: ");
		flexTable.setWidget(0, 0, lblName);
		
		final TextBox txtbxName = new TextBox();
		txtbxName.setText("");
		flexTable.setWidget(0, 1, txtbxName);
		
		Label lblDescription = new Label("description:");
		flexTable.setWidget(1, 0, lblDescription);
		
		final TextArea txtrDesc = new TextArea();
		flexTable.setWidget(1, 1, txtrDesc);
		
		Label lblPrice = new Label("price:");
		flexTable.setWidget(2, 0, lblPrice);
		flexTable.getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_TOP);
		
		txtPrice = new DoubleBox();
		flexTable.setWidget(2, 1, txtPrice);
		flexTable.getCellFormatter().setHorizontalAlignment(2, 1, HasHorizontalAlignment.ALIGN_LEFT);
		
		Label lblDiscount = new Label("discount:");
		flexTable.setWidget(3, 0, lblDiscount);
		
		final IntegerBox txtDiscount = new IntegerBox();
		flexTable.setWidget(3, 1, txtDiscount);
		
		Label lblImage = new Label("Image:");
		flexTable.setWidget(4, 0, lblImage);
		
		final FileUpload fUImage = new FileUpload();
		flexTable.setWidget(4, 1, fUImage);
		
		Button btnAddProduct = new Button("Add Product");
		btnAddProduct.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				insertProduct(txtbxName.getText(), txtrDesc.getText(),Double.parseDouble(txtPrice.getText()), Integer.parseInt(txtDiscount.getText()), fUImage.getFilename());
			}
		});
		flexTable.setWidget(5, 1, btnAddProduct);
	}


private void insertProduct(String name, String desc, double price,int discount,String img) 
{
	// Initialize the service proxy.
	    if (marketingSvc == null) {
	    	marketingSvc = GWT.create(MarketingService.class);
	    }

	    // Set up the callback object.
	    AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
	      public void onFailure(Throwable caught) {
	    	  // If the stock code is in the list of delisted codes, display an error message.
	          //String details = caught.getMessage();
	  		Window.alert(caught.getMessage());

	         }

	      public void onSuccess(Boolean result) {
	    	  /*txtbxName.setText("");
	    	  modelTextBox.setText("");
	    	  priceTextBox.setText("");
	    	  discountTextBox.setText("");
	    	  imgTextBox.setText("");
	    	  */
	      }
	    };

	    // Make the call to the stock price service.
	    marketingSvc.insertProduct(name, desc, price, discount, img, callback);
 }}
 